package ca.ubc.cs304.library.actions;

import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import ca.ubc.cs304.library.gui.LibWindow;

public class ClerkActionsDelegate {
	private LibWindow applicationWindow;
	private final String CLERK_WARNING_ICON_FILEPATH = "assets/warning_icon.png";
	private final String CLERK_SUCCESSFUL_ICON_FILEPATH = "assets/check_mark_icon.png";
	private final String CLERK_CHECKOUT_SUCCESSFUL_ICON_FILEPATH = "assets/check_out_icon.png";
	private final String CLERK_DOES_NOT_EXIST_FILEPATH = "assets/does_not_exist.png";
	private final String CLERK_EMAIL_FILEPATH = "assets/envelope.png";
	private final String CLERK_FINE_FILEPATH = "assets/money.png";
	
	public ClerkActionsDelegate() {
		applicationWindow = LibWindow.getInstance();
	}
	
	public void createPopup(String messageShown, String filePathOfIcon, String createDialogueMessage) {
		JFrame frame = applicationWindow.getFrame();

		JOptionPane pane = new JOptionPane(messageShown);
		ImageIcon imageIcon = new ImageIcon(filePathOfIcon);
		pane.setIcon(imageIcon);
		JDialog dialog = pane.createDialog(createDialogueMessage);
		dialog.setLocation(frame.getX() + (frame.getWidth() - dialog.getWidth()) / 2, frame.getY() + (frame.getHeight() - dialog.getHeight()) / 2);
		dialog.setVisible(true);
	}
	
	public void addBorrowerDidFinish(int bid) {
		String messageShown = "Borrower has been succesfully added.  The borrower's bid is: " + bid;
		String filePathOfIcon = CLERK_SUCCESSFUL_ICON_FILEPATH;
		String createDialogueMessage = "Clerk Action Successful";
		
		createPopup(messageShown, filePathOfIcon, createDialogueMessage);
        
        (applicationWindow.getSinOrStudentNumberTextField()).setText("");
        (applicationWindow.getNameTextField()).setText("");
        (applicationWindow.getPasswordTextField()).setText("");
        (applicationWindow.getAddressTextField()).setText("");
        (applicationWindow.getPhoneNumberTextField()).setText("");
        (applicationWindow.getEmailAddressTextField()).setText("");
        (applicationWindow.getExpiryDateTextField()).setText("");
        if (applicationWindow.getUserTypeComboBox().getItemCount() > 0) {
            (applicationWindow.getUserTypeComboBox()).setSelectedIndex(0);
        }
	}
	
	public void checkOutDidFinish() {
		String messageShown = "Books have been successfully checked out.";
		String filePathOfIcon = CLERK_CHECKOUT_SUCCESSFUL_ICON_FILEPATH;
		String createDialogueMessage = "Clerk Action Successful";
		
		createPopup(messageShown, filePathOfIcon, createDialogueMessage);

		(applicationWindow.getBorrowerIdTextField()).setText("");
		JTextField[] callNumberTextFields = applicationWindow.getCallNumTextFields();
		JTextField[] copyNumberTextFields = applicationWindow.getCopyNumberTextFields();
		for(JTextField textField: callNumberTextFields) {
			textField.setText("");
		}
		for(JTextField textField: copyNumberTextFields) {
			textField.setText("");
		}
	}
	
	public void processReturnSuccessfully() {
		String messageShown = "This book has been successfully returned.";
		String filePathOfIcon = CLERK_SUCCESSFUL_ICON_FILEPATH;
		String createDialogueMessage = "Clerk Action Successful";
		
		createPopup(messageShown, filePathOfIcon, createDialogueMessage);
		
		(applicationWindow.getBorrowerIdTextField()).setText("");
		applicationWindow.getCallNumTextFields()[0].setText("");
		applicationWindow.getCopyNumberTextFields()[0].setText("");
	}
	
	public void borrowerNotInSystem() {
		//TODO
		System.out.println("Borrower not found in system.");
	}
	
	public void addBorrowerDidReceiveDatabaseIsNotConnected() {
        didReceiveDatabaseIsNotConnected();
	}
    
    public void checkOutDidReceiveDatabaseIsNotConnected() {
        didReceiveDatabaseIsNotConnected();
    }
    
    public void processReturnDidReceiveDatabaseIsNotConnected() {
        didReceiveDatabaseIsNotConnected();
    }
    
    public void checkOverdueDidReceiveDatabaseIsNotConnected() {
        didReceiveDatabaseIsNotConnected();
    }
    
    private void didReceiveDatabaseIsNotConnected() {
    	String messageShown = "There is no database connection.";
		String filePathOfIcon = CLERK_WARNING_ICON_FILEPATH;
		String createDialogueMessage = "Clerk Action Error";
		
		createPopup(messageShown, filePathOfIcon, createDialogueMessage);
    }

	public void borrowerTypeDoesNotExist() {
		String messageShown = "This borrower type does not exist in the library database.";
		String filePathOfIcon = CLERK_DOES_NOT_EXIST_FILEPATH;
		String createDialogueMessage = "Clerk Action Error";
		
		createPopup(messageShown, filePathOfIcon, createDialogueMessage);
	}

	public void callNumberDoesNotExist() {
		String messageShown = "One or more call numbers do not exist in the library database.";
		String filePathOfIcon = CLERK_DOES_NOT_EXIST_FILEPATH;
		String createDialogueMessage = "Clerk Action Error";
		
		createPopup(messageShown, filePathOfIcon, createDialogueMessage);
	}

	public void bookAlreadyOut() {
		String messageShown = "Please check the call number(s) listed as one or more call numbers have already been signed out of the library.";
		String filePathOfIcon = CLERK_DOES_NOT_EXIST_FILEPATH;
		String createDialogueMessage = "Clerk Action Error";
		
		createPopup(messageShown, filePathOfIcon, createDialogueMessage);
	}

	public void emailBidOnHold(int bid, String bidOnHoldEmail) {
		String messageShown = "The borrower with bid " + bid + " and email address " + bidOnHoldEmail + " has placed a hold on this item.  Press \"Ok\" to email the user.";
		String filePathOfIcon = CLERK_EMAIL_FILEPATH;
		String createDialogueMessage = "Clerk Action";
		
		createPopup(messageShown, filePathOfIcon, createDialogueMessage);
	}

	public void bookNotOut() {
		String messageShown = "Please check the call number.  It is not registered as \"Out\" in the library database.";
		String filePathOfIcon = CLERK_DOES_NOT_EXIST_FILEPATH;
		String createDialogueMessage = "Clerk Action Error";
		
		createPopup(messageShown, filePathOfIcon, createDialogueMessage);
	}

	public void bookOverdue() {
		String messageShown = "This book has been returned after the due date.  User has been recorded in the Fines table.";
		String filePathOfIcon = CLERK_FINE_FILEPATH;
		String createDialogueMessage = "Clerk Action";
		
		createPopup(messageShown, filePathOfIcon, createDialogueMessage);

	}

	public void dateNotFound(String date) {
		String messageShown = "Sorry.  An error has occured with trying to find the " + date + " of this return.  Please contact the system administrator.";
		String filePathOfIcon = CLERK_DOES_NOT_EXIST_FILEPATH;
		String createDialogueMessage = "Clerk Action Error";
		
		createPopup(messageShown, filePathOfIcon, createDialogueMessage);
	}

	public void sinOrStNumNotInt() {
		String messageShown = "Sorry.  The \"Sin or Student #\" field does not have a integer value entered.";
		String filePathOfIcon = CLERK_DOES_NOT_EXIST_FILEPATH;
		String createDialogueMessage = "Clerk Action Error";
		
		createPopup(messageShown, filePathOfIcon, createDialogueMessage);
	}

	public void bidNotInSystem(String bid) {
		String messageShown = "Sorry.  The bid " + bid + " does not exist in the library database.";
		String filePathOfIcon = CLERK_DOES_NOT_EXIST_FILEPATH;
		String createDialogueMessage = "Clerk Action Error";
		
		createPopup(messageShown, filePathOfIcon, createDialogueMessage);
	}
}
